//
//  WCPTotalLoanInfoHeaderView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/17.
//

#import "WCPTotalLoanInfoHeaderView.h"

@implementation WCPTotalLoanInfoHeaderView

#pragma mark - lazy
- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView  new];
        _viewBack.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
        _viewBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    }
    return  _viewBack;
}
- (UILabel *)labelLeftTitle{
    if (_labelLeftTitle == nil) {
        _labelLeftTitle = [UILabel  new];
        _labelLeftTitle.textColor = COLOR_BLACK;
        _labelLeftTitle.fontNum = 12;
        [_labelLeftTitle fitTitle:@"Monto del préstamo($)" variable:0];
    }
    return  _labelLeftTitle;
}
- (UILabel *)labelLeftMoney{
    if (_labelLeftMoney == nil) {
        _labelLeftMoney = [UILabel  new];
        _labelLeftMoney.textColor = COLOR_BLACK;
        _labelLeftMoney.font = [UIFont boldSystemFontOfSize:20];
    }
    return  _labelLeftMoney;
}
- (UILabel *)labelRightTitle{
    if (_labelRightTitle == nil) {
        _labelRightTitle = [UILabel  new];
        _labelRightTitle.textColor = COLOR_BLACK;
        _labelRightTitle.fontNum = 12;
        [_labelRightTitle fitTitle:@"Plazo del préstamo(Días)" variable:0];
    }
    return  _labelRightTitle;
}
- (UILabel *)labelRightMoney{
    if (_labelRightMoney == nil) {
        _labelRightMoney = [UILabel  new];
        _labelRightMoney.textColor = COLOR_BLACK;
        _labelRightMoney.font = [UIFont boldSystemFontOfSize:20];
    }
    return  _labelRightMoney;
}
- (UIView *)lineView{
    if (_lineView == nil) {
        _lineView = [UIView  new];
        _lineView.width = 1;
        _lineView.backgroundColor = COLOR_LINE;
    }
    return  _lineView;
}
- (UILabel *)labelTime
{
    if (_labelTime == nil) {
        _labelTime = [UILabel  new];
        _labelTime.textColor = COLOR_WHITE;
        _labelTime.fontNum = 14;
        [_labelTime fitTitle:@"01:00" variable:0];
    }
    return  _labelTime;
}
- (UIButton *)btnSubmit{
    if (_btnSubmit == nil) {
        _btnSubmit = [UIButton  new];
        _btnSubmit.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnSubmit color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        [_btnSubmit setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
        _btnSubmit.titleLabel.fontNum = 14;
        [_btnSubmit setTitle:@"Solicitar ahora" forState:UIControlStateNormal];
        _btnSubmit.backgroundColor = COLOR_BLACK;
        _btnSubmit.tag = 1;
        [_btnSubmit addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnSubmit;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.labelLeftTitle];
    [self.viewBack addSubview:self.labelLeftMoney];
    [self.viewBack addSubview:self.lineView];
    [self.viewBack addSubview:self.labelRightTitle];
    [self.viewBack addSubview:self.labelRightMoney];
    [self addSubview:self.btnSubmit];
    [self.btnSubmit addSubview:self.labelTime];
    [self resetView:nil];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            if(self.blockSubmint){
                self.blockSubmint();
            }
        }
            break;
        default:
            break;
    }
}

// 开始倒计时
- (void)startCountdownTime{
    if (self.timer) {
        dispatch_source_cancel(self.timer);
        self.timer = nil;
    }
    //倒计时时间
    __block NSInteger timeOut = 60;
    //建一个全局队列，默认优先级
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(self.timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
        
        //倒计时结束，关闭
        if (timeOut <= 0) {
            dispatch_source_cancel(self.timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self endCountdownTime];
            });
        } else {
            int seconds = timeOut % 60;
            NSInteger firstNum = timeOut/ 60;
            NSString * firstStr = firstNum>0?@"01":@"00";
            
            NSString *timeStr = [NSString stringWithFormat:@"%0.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.labelTime fitTitle:[NSString stringWithFormat:@"%@:%@",firstStr,timeStr] variable:0];
                self.labelTime.leftCenterY = XY(30, self.btnSubmit.height/2);
            });
            timeOut--;
        }
    });
    dispatch_resume(self.timer);
}

// 倒计时结束
- (void)endCountdownTime{
    if (self.timer) {
        dispatch_source_cancel(self.timer);
        self.timer = nil;
    }
    self.labelTime.hidden = true;
}

#pragma mark - 刷新View
- (void)resetView:(ModelBtn *)model{
    self.labelLeftTitle.centerXTop = XY(self.viewBack.width/4, 26);
    [self.labelLeftMoney fitTitle:readStr(model.title) variable:0];
    //self.labelLeftMoney.leftTop = XY(self.labelLeftTitle.left, self.labelLeftTitle.bottom+7);
    self.labelLeftMoney.centerXTop = XY(self.labelLeftTitle.centerX, self.labelLeftTitle.bottom+7);
    
    self.labelRightTitle.centerXCenterY = XY(self.viewBack.width/4*3, self.labelLeftTitle.centerY);
    [self.labelRightMoney fitTitle:readStr(model.content) variable:0];
    //self.labelRightMoney.rightCenterY = XY(self.labelRightTitle.right, self.labelLeftMoney.centerY);
    self.labelRightMoney.centerXCenterY = XY(self.labelRightTitle.centerX, self.labelLeftMoney.centerY);
    
    self.viewBack.height = self.labelLeftMoney.bottom+self.labelLeftTitle.top;
    self.viewBack.centerXTop = XY(self.width/2, 10);
    self.lineView.height = self.viewBack.height-29-29;
    self.lineView.centerXCenterY = XY(self.viewBack.width/2, self.viewBack.height/2);
    
    self.btnSubmit.centerXTop = XY(self.width/2, self.viewBack.bottom+18);
    
    self.height = self.btnSubmit.bottom+LEFT_MARDIN;
}

@end
